<?php
/*
 * @author eBIZ Team <dev@jungo.com.cn>
 * @copyright  Copyright (c) , ShenZhen JunGO Technology Co., Ltd. All rights reserved.
 * @license  Commercial authorization, unauthorized use and modification are prohibited
 * @url www.jungo.com.cn
 */

namespace ckeeper\utils;


class PasswordEncrypt
{

    /**
     * @param string $password
     * @param string $salt
     * @return string
     */
    public static function encrypt(string $password, string $salt): string
    {
        return sha1(sha1($password . $salt . $password . $salt) . sha1($password, true));
    }


    /**
     * 检测密码是否正确
     * @param string $password
     * @param string $encryptString
     * @param string $salt
     * @return bool
     */
    public static function checking(string $password, string $encryptString, string $salt): bool
    {
        $newEncryptString = self::encrypt($password, $salt);
        return $newEncryptString === $encryptString;
    }


    /**
     * 随机生成加盐字符串
     * @return string
     */
    public static function randomGenSalt(): string
    {
        $code = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
            'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z','.','-','_','!','@','#','$','%','^','&','*','(',')','+',
            '[',']','{','}','【','】','「','」','《','》','?','/','\\','"','\'',',','，',';',':','、','|'];
        $length = rand(3, 12);
        $salt = '';
        for ($i = 0; $i < $length; $i++) {
            $salt .= $code[rand(0, count($code) - 1)];
        }
        return $salt;
    }


}
